package com.huatai.auth.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huatai.user.entity.User;
import com.huatai.user.vo.UserInfoVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author songxiude
 */
public interface UserMapper extends BaseMapper<User> {

	/**
	 * 获取用户权限列表
	 * @param id
	 * @return
	 */
	@Select("SELECT permission_key FROM sys_permission WHERE id IN(" +
		"SELECT DISTINCT permission_id FROM sys_role_permission WHERE role_id IN(" +
		"SELECT role_id FROM sys_user_role WHERE user_id=#{id})) AND is_show=0 AND permission_type=2")
	List<String> selcetUserPermissions(@Param("id") Long id);

	/**
	 * 超级管理员获取全部可用权限接口
	 * @return
	 */
	@Select("SELECT permission_key FROM sys_permission WHERE is_show=0 AND permission_type=2")
	List<String> selectAdminPers();
	/**
	 * 获取用户列表
	 * @param page
	 * @param wrapper
	 * @return
	 */
	@Select("SELECT u.id,u.code,u.account,u.name,u.real_name,u.avatar,u.email,u.phone,u.birthday,u.sex,u.is_admin,u.dept_id,u.post_id,u.status,d.dept_name as deptname,p.post_name as postname,u.create_time,u.update_time " +
		"FROM sys_user u LEFT JOIN sys_department d ON u.dept_id=d.id LEFT JOIN sys_post p ON u.post_id=p.id ${ew.customSqlSegment}")
	IPage<UserInfoVo> selectUserList(Page<UserInfoVo> page, @Param(Constants.WRAPPER) LambdaQueryWrapper wrapper);


	/**
	 * 获取用户角色列表id
	 * @param userId
	 * @return
	 */
	@Select("SELECT DISTINCT role_id FROM sys_user_role WHERE user_id=#{userId}")
	List<Long> getUserRoleIds(@Param("userId") Long userId);
}
